#!/bin/bash -ile

######################################################

#Jenkins任务名称
JobName=shop-admin-vue

#站点部署路径
SiteRoot=/www/wwwroot/shop.javachmapi.com

#构建产物路径
WorkspaceRoot=/www/server/.jenkins/workspace

#构建文件夹名称
Generate=dist

######################################################

#进入Jenkins任务名称
cd /www/server/.jenkins/workspace/$JobName

echo "在清除旧数据······"
rm -rf Generate/
rm -rf node_modules/

echo "正在安装依赖······"
npm install --registry=https://registry.npm.taobao.org
npm install --unsafe-perm webpack@4.35.2
npm install

echo "正在编译程序······"
source /etc/profile
npm run build

if [ ! -d "$WorkspaceRoot/$JobName/$Generate/*" ];then
  echo "编译程序失败！"
  else
  echo "编译程序完成！"
fi

if [ "`ls -A ${SiteRoot}`" = "" ];then
  echo "站点数据为空！"
  mv $Generate/* $SiteRoot
else
  echo "站点数据非空！"
  rm -rf $SiteRoot/*
  mv $Generate/* $SiteRoot
fi

echo "自动化部署成功！"
